package com.github.smartretry.spring4.registry.distributejob;

import com.aliyun.gts.bpaas.schedule.api.AbstractGtsSchedulerTaskProcessor;
import com.aliyun.gts.bpaas.schedule.common.GtsSchedulerTaskParameter;
import com.aliyun.gts.bpaas.schedule.common.GtsSchedulerTaskResult;
import com.github.smartretry.core.RetryHandler;
import com.github.smartretry.core.RetryProcessor;
import com.github.smartretry.core.RetryRegistry;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.InitializingBean;
import org.springframework.stereotype.Component;

import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

/**
 * @author cxk
 * @date 2020/9/25 0025 11:07
 */

@Slf4j
public class DistributeRetryRegistry extends AbstractGtsSchedulerTaskProcessor implements RetryRegistry {

    
    @Override
    public String getTaskId(){
        return "retry-job";
    }

    private List<RetryProcessor> retryProcessorList;

    @Override
    public GtsSchedulerTaskResult process(GtsSchedulerTaskParameter parameter) throws Exception{
        DateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        log.info("执行定时任务，当前时间{}",format.format(new Date()));
        retryProcessorList.forEach(RetryProcessor::doRetry);
        return GtsSchedulerTaskResult.successResult();
    }

    @Override
    public void register(RetryHandler retryHandler, List<RetryProcessor> retryProcessorList) {
        this.retryProcessorList = retryProcessorList;
    }
}
